#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2021/12/29 11:42
    Desc  :
--------------------------------------
"""
from common.exceptions import MicroseerEnvNotExist, MicroseerLoginFail, MicroseerCidNotExist
from common.tools.CommonRequestsClient import PostRequestClient
from extends import yamlUtils


def moa_login(env: str, mscid):
    """moa登陆"""

    env_dict = yamlUtils.get(f'{env}')
    if env_dict:
        mscid_dict = env_dict.get(f'{mscid}')

        if mscid_dict:
            username = mscid_dict.get('username')
            password = mscid_dict.get('password')
            requestUtils = PostRequestClient()
            requestUtils.url = env_dict.get('url') + '/auth'
            requestUtils.body = {
                'userName': username,
                'password': password
            }
            result = requestUtils.result()
            result = result.json()
            if result.get('data'):
                token = result.get('data').get('token')
                return token
            else:
                raise MicroseerEnvNotExist(f"登陆失败-{result.get('detail')}")
        else:
            raise MicroseerCidNotExist(f"cid = {mscid} 不存在")
    else:
        raise MicroseerEnvNotExist(f"环境 = {env} 不存在")